package com.letfox.ad.controller.finance;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.letfox.ad.controller.BaseController;
import com.letfox.ad.service.FINAccountNoviceService;
import com.letfox.ad.service.FINComprehensiveService;
import com.letfox.common.enums.ResponseEnum;
import com.letfox.common.model.ResultResponse;
import com.letfox.finance.model.FINAccount;
import com.letfox.finance.model.FINNoviceInfo;
import com.letfox.finance.service.impl.WXPayServiceImpl;
import com.letfox.finance.wxpay.util.WeChatParams;

@Controller
@RequestMapping(value = "/finance")
public class FinanceControlle extends BaseController{
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private FINComprehensiveService finComprehensiveService;
	@Autowired
	private FINAccountNoviceService finAccountNoviceService;
	/**
	 * 查询账户信息
	 * @param accountId
	 * @return
	 */
	@RequestMapping(value = "/getfinAm",method=RequestMethod.POST)
	@ResponseBody
	public ResultResponse getFinAccount(@RequestParam String accountId){
		FINAccount finAccount=null;
		try {
			finAccount=finComprehensiveService.getFinAccount(accountId);
			resultResponse.setData(finAccount);
			resultResponse.setStatusCode(ResponseEnum.R200.getCode());
			resultResponse.setStatusMessage(ResponseEnum.R200.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			resultResponse.setStatusCode(ResponseEnum.R500.getCode());
			resultResponse.setStatusMessage(ResponseEnum.R500.getMessage());
		}
		return resultResponse;
	}
	
	/**
	 * 根据财务账户查询消费信息
	 * @return
	 */
	@RequestMapping(value = "/getFinCsp",method=RequestMethod.POST)
	@ResponseBody
	public ResultResponse getFinConsumption(@RequestParam String finAccountId,@RequestParam String strTime,
			@RequestParam String endTime){
		Map<String, Object> map=null;
		try {
			map=finComprehensiveService.accountDataConsumption(finAccountId, strTime, endTime);
			resultResponse.setData(map);
			resultResponse.setStatusCode(ResponseEnum.R200.getCode());
			resultResponse.setStatusMessage(ResponseEnum.R200.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			resultResponse.setStatusCode(ResponseEnum.R500.getCode());
			resultResponse.setStatusMessage(ResponseEnum.R500.getMessage());
		}
		return resultResponse;
	}
	
	/**
	 * 存储发票信息 
	 * @param finNoviceInfo
	 * @param finInoutId
	 * @return
	 */
	@RequestMapping(value = "/saveNovice",method=RequestMethod.POST)
	@ResponseBody
	public ResultResponse saveFinNovice(@ModelAttribute FINNoviceInfo finNoviceInfo,@RequestParam String finInoutId){
		int i=0;
		try {
			i=finAccountNoviceService.saveFinNovice(finNoviceInfo, finInoutId);
			resultResponse.setData(i);
			resultResponse.setStatusCode(ResponseEnum.R200.getCode());
			resultResponse.setStatusMessage(ResponseEnum.R200.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			resultResponse.setStatusCode(ResponseEnum.R500.getCode());
			resultResponse.setStatusMessage(ResponseEnum.R500.getMessage());
		}
		return resultResponse;
	}
 	
	/**
	 * 查询某账户下所有的发票信息
	 * @param finAccountId
	 * @return
	 */
	@RequestMapping(value = "/getNovices",method=RequestMethod.POST)
	@ResponseBody
	public ResultResponse getAcountNovices(@RequestParam String finAccountId){
		List<FINNoviceInfo> noviceInfos=null;
		try {
			noviceInfos=finAccountNoviceService.getAcctountNoviceInfos(finAccountId);
			resultResponse.setData(noviceInfos);
			resultResponse.setStatusCode(ResponseEnum.R200.getCode());
			resultResponse.setStatusMessage(ResponseEnum.R200.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			resultResponse.setStatusCode(ResponseEnum.R500.getCode());
			resultResponse.setStatusMessage(ResponseEnum.R500.getMessage());
		}
		return resultResponse;
	}
	
	@RequestMapping(value="/getPay",method=RequestMethod.POST)
	public void getPay(@ModelAttribute WeChatParams weChatParams){
		WXPayServiceImpl wxpay=new WXPayServiceImpl();
		weChatParams.setBody(null);
		try {
			wxpay.pay(response, weChatParams);
//		    BufferedImage image = ImageIO.read(new FileInputStream("C:\\Users\\Administrator\\Desktop\\fjtp.png")); 
//			response.setContentType("image/png");
//			OutputStream os = response.getOutputStream();
//			ImageIO.write(image, "png", os);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}	
	}
	
	@RequestMapping(value="/payback",method=RequestMethod.GET)
	public void getPayback(){
		WXPayServiceImpl wxpay=new WXPayServiceImpl();
		try {
			wxpay.wechat_notify_url_pc(request, response);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}	
	}
}
